package com.atguigu.gmall.product.service.impl;
import com.atguigu.gmall.feign.search.SearchFeignClients;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.mapper.BaseCategory2Mapper;
import com.atguigu.gmall.product.service.*;
import com.atguigu.gmall.product.vo.CategoryTreeVo;
import com.atguigu.gmall.search.bean.SearchAttr;
import com.google.common.collect.Lists;
import java.util.Date;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.vo.SkuSaveInfoVo;
import com.atguigu.gmall.search.bean.Goods;
import com.atguigu.gmall.search.vo.SearchRespVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.mapper.SkuInfoMapper;
import io.minio.messages.Item;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo>
        implements SkuInfoService {

    @Autowired
    SkuAttrValueService skuAttrValueService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    BaseTrademarkService baseTrademarkService;
    @Autowired
    BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    SearchFeignClients searchFeignClients;



    @Override
    public void saveSkuInfo(SkuSaveInfoVo vo) {
        SkuInfo skuInfo = new SkuInfo();
        BeanUtils.copyProperties(vo, skuInfo);
        save(skuInfo);
        Long skuInfoId = skuInfo.getId();

        List<SkuSaveInfoVo.SkuAttrValueListDTO> skuAttrValueList = vo.getSkuAttrValueList();
        List<SkuAttrValue> skuAttrValues = skuAttrValueList.stream().map(item -> {
            SkuAttrValue skuAttrValue = new SkuAttrValue();
            skuAttrValue.setSkuId(skuInfoId);
            BeanUtils.copyProperties(item, skuAttrValue);
            return skuAttrValue;
        }).collect(Collectors.toList());

        skuAttrValueService.saveBatch(skuAttrValues);


        List<?> skuSaleAttrValueList = vo.getSkuSaleAttrValueList();
        List<SkuSaleAttrValue> skuSaleAttrValues = skuSaleAttrValueList.stream().map(item -> {
            SkuSaleAttrValue skuSaleAttrValue = new SkuSaleAttrValue();
            BeanUtils.copyProperties(item, skuSaleAttrValue);
            skuSaleAttrValue.setSkuId(skuInfoId);

            return skuSaleAttrValue;
        }).collect(Collectors.toList());

        skuSaleAttrValueService.saveBatch(skuSaleAttrValues);

        List<SkuSaveInfoVo.SkuImageListDTO> skuImageList = vo.getSkuImageList();
        List<SkuImage> skuImages = skuImageList.stream().map(item -> {
            SkuImage skuImage = new SkuImage();
            BeanUtils.copyProperties(item, skuImage);
            skuImage.setSkuId(skuInfoId);
            return skuImage;
        }).collect(Collectors.toList());

        skuImageService.saveBatch(skuImages);


        //同步bitmap
        redisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP,skuInfoId,true);
    }

    /**
     * 商品上架
     * @param skuId
     */
    @Override
    public void onSale(Long skuId) {
        boolean update = lambdaUpdate().set(SkuInfo::getIsSale, 1).eq(SkuInfo::getId, skuId).update();
        if (update){
            Goods goods = prepareGoods(skuId);
            searchFeignClients.up(goods);
        }
    }

    /**
     * 封装Goods
     * @param skuId
     * @return
     */
    private Goods prepareGoods(Long skuId) {
        SkuInfo skuInfo = getById(skuId);
        Goods goods = new Goods();
        goods.setId(skuId);
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        goods.setPrice(skuInfo.getPrice().doubleValue());
        goods.setCreateTime(new Date());
        goods.setTmId(skuInfo.getTmId());
        BaseTrademark baseTrademark = baseTrademarkService.getById(skuInfo.getTmId());
        goods.setTmName(baseTrademark.getTmName());
        goods.setTmLogoUrl(baseTrademark.getLogoUrl());

        CategoryTreeVo tree = baseCategory2Mapper.getCategoryTreeWithC3Id(skuInfo.getCategory3Id());

        goods.setCategory1Id(tree.getCategoryId());
        goods.setCategory1Name(tree.getCategoryName());
        CategoryTreeVo child = tree.getCategoryChild().get(0);
        goods.setCategory2Id(child.getCategoryId());
        goods.setCategory2Name(child.getCategoryName());
        CategoryTreeVo child2 = child.getCategoryChild().get(0);
        goods.setCategory3Id(child2.getCategoryId());
        goods.setCategory3Name(child2.getCategoryName());

        goods.setHotScore(0L);

        List<SearchAttr> searchAttrs =  skuAttrValueService.getSkuAttrsAndValue(skuId);

        goods.setAttrs(searchAttrs);
        return goods;
    }


    /**
     * 商品下架
     * @param skuId
     */
    @Override
    public void cancelSale(Long skuId) {

        lambdaUpdate().set(SkuInfo::getIsSale,0).eq(SkuInfo::getId,skuId).update();

        searchFeignClients.down(skuId);
    }
}




